package max.t;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Calculator2Activity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    Button button1;
	Button button2;
	Button button3;
	Button button4;
	Button button5;
	Button button6;
	Button button7;
	Button button8;
	Button button9;
	Button button0;
	Button buttondecimal;
	Button buttonadd;
	Button buttonsubtract;
	Button buttondivide;
	Button buttonmultiply;
	EditText editText1;
	Button buttonclear;
	Button buttonenter;
	double firstnumber=0;
	double secondnumber=0;
	int abc=0;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calcmain);
        
        
        buttonenter = (Button) findViewById(R.id.buttonenter);
        buttonenter.setText("=");
        buttonenter.setOnClickListener(this);
        
        editText1 = (EditText) findViewById(R.id.editText1);
        
        buttonclear = (Button) findViewById(R.id.buttonclear);
        buttonclear.setText("Clear");
        buttonclear.setOnClickListener(this);
        
        button1 = (Button) findViewById(R.id.button1);
        button1.setText("1");
        button1.setOnClickListener(this);
     
        button2 = (Button) findViewById(R.id.button2);
        button2.setText("2");
        button2.setOnClickListener(this);
        
        button3 = (Button) findViewById(R.id.button3);
        button3.setText("3");
        button3.setOnClickListener(this);
        
        button4 = (Button) findViewById(R.id.button4);
        button4.setText("4");
        button4.setOnClickListener(this);
        
        button5 = (Button) findViewById(R.id.button5);
        button5.setText("5");
        button5.setOnClickListener(this);
       
        button6 = (Button) findViewById(R.id.button6);
        button6.setText("6");
        button6.setOnClickListener(this);
        
        button7 = (Button) findViewById(R.id.button7);
        button7.setText("7");
        button7.setOnClickListener(this);
        
        button8 = (Button) findViewById(R.id.button8);
        button8.setText("8");
        button8.setOnClickListener(this);
        
        button9 = (Button) findViewById(R.id.button9);
        button9.setText("9");
        button9.setOnClickListener(this);
       
        button0 = (Button) findViewById(R.id.button0);    
        button0.setText("0");
        button0.setOnClickListener(this);
    
        
        buttondecimal = (Button) findViewById(R.id.buttondecimal);
        buttondecimal.setText(".");
        buttondecimal.setOnClickListener(this);
        
        buttonadd = (Button) findViewById(R.id.buttonadd);
        buttonadd.setText("+");
        buttonadd.setOnClickListener(this);
        
        buttonsubtract = (Button) findViewById(R.id.buttonsubtract);
        buttonsubtract.setText("-");
        buttonsubtract.setOnClickListener(this);
        
        buttonmultiply = (Button) findViewById(R.id.buttonmultiply);
        buttonmultiply.setText("X");
        buttonmultiply.setOnClickListener(this);
        
        buttondivide = (Button) findViewById(R.id.buttondivide);
        buttondivide.setText("/");
        buttondivide.setOnClickListener(this);
    }

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.equals(button1)){
			editText1.append("1");
		}
		
		if(v.equals(button2)){
			editText1.append("2");
		}
		if(v.equals(button3)){
			editText1.append("3");
		}
		if(v.equals(button4)){
			editText1.append("4");
		}
		if(v.equals(button5)){
			editText1.append("5");
		}
		if(v.equals(button6)){
			editText1.append("6");
		}
		if(v.equals(button7)){
			editText1.append("7");
		}
		if(v.equals(button8)){
			editText1.append("8");
		}
		if(v.equals(button9)){
			editText1.append("9");
		}
		if(v.equals(button0)){
			editText1.append("0");
		}
		if(v.equals(buttondecimal)&& (editText1.getText().toString().contains(".") == false) ) {
		
		
		editText1.append(".");
	
        
	 	
		}
		 if(v.equals(buttonadd)){
	       firstnumber = Double.parseDouble(editText1.getText().toString()); 
	       editText1.setText("");
	        abc =1; 
		}
		else if(v.equals(buttonsubtract)){
			firstnumber = Double.parseDouble(editText1.getText().toString()); 
			editText1.setText("");
			 abc =2;
		}
		else if(v.equals(buttonmultiply)){
			firstnumber = Double.parseDouble(editText1.getText().toString()); 
			editText1.setText("");
			 abc =3;
		}
		else if(v.equals(buttondivide)){
		firstnumber = Double.parseDouble(editText1.getText().toString()); 
			editText1.setText("");
			 abc=4;
		}
		if(v.equals(buttonclear)){
			editText1.setText("");
		}
		
		
		if(v.equals(buttonenter))
		{
		secondnumber = Double.parseDouble(editText1.getText().toString());
		
		
		if(abc==1)
		{
		editText1.setText(Double.toString(firstnumber+secondnumber));
		Toast.makeText(this,"Add", Toast.LENGTH_SHORT).show();	
		
		}
		if(abc==2)
		{
			editText1.setText(Double.toString(firstnumber-secondnumber));
			Toast.makeText(this,"Subtract", Toast.LENGTH_SHORT).show();	
		}
       if(abc==3)
       {
    	   editText1.setText(Double.toString(firstnumber*secondnumber));
    	   Toast.makeText(this,"Multiply", Toast.LENGTH_SHORT).show();	
       }
       if(abc==4)
       {
    	   editText1.setText(Double.toString(firstnumber/secondnumber));
       Toast.makeText(this,"Divide", Toast.LENGTH_SHORT).show();	
		
		}
	
}
}
	
}

		
	

	
       
     

